package com.gjintel.workflow.delegate;

import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.gjintel.business.proj.model.ProjectInfo;
import com.gjintel.business.proj.service.IProjectInfoService;
import com.gjintel.business.proj.service.impl.ProjectInfoServiceImpl;
import com.gjintel.framework.constant.ProjectStatus;

/**
 * @description 投标数量校验 (不足三家，自动流标) 
 * @author wwang 
 * @date 2016-8-8
 */
public class BidCountDelegate implements JavaDelegate {

	
	@Autowired
	private IProjectInfoService projectInfoService;
	
	private static int FLOW_DOWN=3,    //流标
	                   SYSTEM_TENDER=2,//系统评标
	                   COMPET_TENDER=1;
	private final Logger log = Logger.getLogger(BidCountDelegate.class.getName());  
	@Override
	public void execute(DelegateExecution execution) throws Exception {
		ApplicationContext ac = new  ClassPathXmlApplicationContext("classpath*:applicationContext.xml"); 
		projectInfoService=(ProjectInfoServiceImpl)ac.getBean("projectInfoServiceImpl");
		
		//Thread.sleep(5000);
		String bkey=execution.getProcessBusinessKey();
		log.info("variavles=" + execution.getVariables());  
		System.out.println("----------------) ");
		System.out.println("bkey is::::::::::::"+bkey);
		//根据bkey判断是否投标数量为三家，不足三家输入input =3
		if(projectInfoService.getProjectBidderByProjId(bkey,false)< 3){
			execution.setVariable("input", FLOW_DOWN);
			ProjectInfo pj=projectInfoService.get(ProjectInfo.class, bkey);
			pj.setStatus(Integer.valueOf(ProjectStatus.FLOW_DOWN.toString()));
			projectInfoService.saveOrUpdate(pj);
		}else {
			ProjectInfo projectInfo=projectInfoService.get(ProjectInfo.class, bkey);//判断项目状态
			boolean auditAble=projectInfo.getAuditable();    //判断 是否是评标
			if(auditAble){
				projectInfo.setStatus(Integer.valueOf(ProjectStatus.SHOTING.toString()));
			    execution.setVariable("input", COMPET_TENDER);
			    projectInfoService.saveOrUpdate(projectInfo);
			    //系统开标提醒：提醒采购人、项目负责人项目已开标
			    
			    
			}
			else{
				execution.setVariable("input", SYSTEM_TENDER);
			}
		}
		
	}

}
